package com.jiesone.logger.distributecleaning.service;
/*
 *@ClassName LoggerMachingService
 *@Description TODO
 *@Author tangsixiang@163.com
 *@Date 2022/2/10 16:03
 *@Version 1.0
 */

import com.alibaba.fastjson.JSON;
import com.jiesone.logger.distributecleaning.config.CommonRediskey;
import com.jiesone.logger.distributecleaning.handler.ResolveMachingRedisHandler;
import com.jiesone.logger.distributecleaning.util.ElasticSearchUtil;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Component
public class LoggerMachingService {
    Logger logger= LoggerFactory.getLogger(LoggerMachingService.class);
    @Autowired
    ElasticSearchUtil searchUtil;
    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 根据加工规则对数据进行业务整合 录入到数据库中
     */
    public   void   receiverMessage( Map<String ,Object>   message) {
        try {
     //       logger.info("---------------进入清洗数--道加工通道 maching-----------------" + message);

            String indeName = message.get("indexName").toString().toLowerCase();

            String[] toIndexStr = redisTemplate.opsForHash().get(CommonRediskey.machingRule, indeName).toString().split(";");
            Map queryMap = new HashMap();
            queryMap.put(toIndexStr[1], message.get(toIndexStr[2]));

            SearchHits resp = searchUtil.searchQuery(queryMap, toIndexStr[0]);

         logger.info("通过查询数据：获取待提取数据查询记录数："+resp.getTotalHits());
            Iterator<SearchHit> it = resp.iterator();
            Map<String, Object> mapes = it.next().getSourceAsMap();
            String[] tmp = toIndexStr[3].split(",");
            Long id = (Long) message.get("id");
            message.remove("id");
            message.remove("indexName");
            for (String str : tmp) {
                if (mapes.containsKey(str)) {
                    message.put(str, mapes.get(str));
                }

            }

       //  logger.info("经过加工后数据 展示---:"+message);



        } catch (Exception exception) {
            exception.printStackTrace();
        }
    //    return message;
    //    return message;
    }


}
